#include "pagebutton.h"

#include <QStyle>
#include "base.h"

PageButton::PageButton(QWidget *parent) : QPushButton(parent) {
    iconLabel = new QLabel(this);
    connect(this, &PageButton::pressed, this, &PageButton::emitClicked);
    connect(this, &PageButton::clicked, this, &PageButton::emitClicked);
}

void PageButton::emitClicked() {
    if (!isChecked()) {
        setChecked(true);
        emit indexChanged(index);
    }
}

void PageButton::setCurrentChosen(int index) {
    if (PageButton::index != index) {
        setChecked(false);
    } else {
        setChecked(true);
    }
}

void PageButton::enterEvent(QEvent *) {
    drawPix(ignitedIconPath);
}

void PageButton::leaveEvent(QEvent *) {
    drawPix(normalIconPath);
}

void PageButton::setIconPath(QString pathNormal, QString pathIgnited) {
    normalIconPath = pathNormal;
    ignitedIconPath = pathIgnited;
}

void PageButton::drawPix(const QString iconPath) {
    iconLabel->setPixmap(iconPath);
    iconLabel->setScaledContents(true);
    iconLabel->resize(iconSize());
    iconLabel->move(iconOffset);
}

QString PageButton::getNormalIconPath() const {
    return normalIconPath;
}

QString PageButton::getIgnitedIconPath() const {
    return ignitedIconPath;
}

void PageButton::setIndex(int index) {
    this->index = index;
}

void PageButton::setIconOffset(int x, int y) {
    iconOffset = QPoint(x, y);
}

int PageButton::Index() const {
    return index;
}
